package com.example.forum.services.impl;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.dao.MessageMapper;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Message;
import com.example.forum.services.IMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName MessageServiceImpl
 * @Description TODO
 * @Author H.
 * @Date 2023/6/26 23:10
 * @Version 1.0
 */
@Slf4j
@Service
public class MessageServiceImpl implements IMessageService {
    @Resource
    private MessageMapper messageMapper;

    @Override
    public void create(Message message) {
        if(message == null || message.getReceiveUserId() == null){
            log.info(ResultCode.ERROR_IS_NULL.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        //设置默认值
        message.setState((byte) 0);//未读状态
        message.setDeleteState((byte) 0);//是否删除
        Date date = new Date();
        message.setCreateTime(date);
        message.setUpdateTime(date);

        //写入数据库
        int row = messageMapper.insertSelective(message);
        if(row != 1){
            log.info(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
        // 记录日志, 记录一下返回Id
        log.info("发送站内信成功：postUserId = " + message.getPostUserId() + ", receiveUserId = " + message.getReceiveUserId());
    }

    @Override
    public List<Message> selectByReceiveUserId(Long receiveUserId) {
        if(receiveUserId == null){
            // 记录日志
            log.info(ResultCode.ERROR_IS_NULL.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        List<Message> messages = messageMapper.selectByReceiveUserId(receiveUserId);
        return messages;
    }

    @Override
    public Integer selectUnreadCount(Long userId) {
        if(userId == null){
            // 记录日志
            log.info(ResultCode.ERROR_IS_NULL.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        Integer result = messageMapper.selectUnreadCount(userId);
        return result;
    }

    @Override
    public void updateById(Message message) {
        if(message == null || message.getId() == null){
            // 记录日志
            log.info(ResultCode.ERROR_IS_NULL.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        int row = messageMapper.updateByPrimaryKeySelective(message);
        if(row != 1){
            // 记录日志
            log.info(ResultCode.FAILED.toString() + "messageId = " + message.getId());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public Message selectById(Long id) {
        if(id == null){
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message message = messageMapper.selectByPrimaryKey(id);
        return message;
    }

    @Override
    public void reply(Long repliedId, Message message) {
        //非空校验
        if(repliedId == null || message == null || message.getReceiveUserId() == null){
            // 记录日志
            log.info(ResultCode.ERROR_IS_NULL.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        Message replied = messageMapper.selectByPrimaryKey(repliedId);
        if(replied == null || replied.getDeleteState() == 1){
            // 记录日志
            log.info(ResultCode.FAILED_NOT_EXISTS.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
        }
        //更新状态
        Message updateMessage = new Message();
        updateMessage.setId(repliedId);
        updateMessage.setUpdateTime(new Date());
        updateMessage.setState((byte) 2);//表示已经回复
        messageMapper.updateByPrimaryKeySelective(updateMessage);

        //写入新的记录
        create(message);
    }


}
